package edu.cicese.alz.reports;

import javax.swing.DefaultListModel;
import java.awt.Color;
import java.util.Date;
import java.util.TreeSet;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 6/08/12
 * Time: 12:20 PM
 */
public class CombinedReport extends Report {

//	private TreeSet<Integer> userIDs;
//	private long tsBegin, tsEnd;
//	private boolean showCharts, saveCharts;

	private DefaultListModel listLayersModel;

	private AccelerometerPane accPane;
	private AccelerometerReport accelerometerReport;
	private GPSPane gpsPane;
	private GPSReport gpsReport;
	private NFCPane nfcPane;
	private NFCReport nfcReport;

	private int[] opacities;

	public CombinedReport(TreeSet<Integer> userIDs, Date beginDate, Date endDate, Date dayBeginDate, Date dayEndDate,
	                      boolean showCharts, boolean saveCharts, DefaultListModel listLayersModel,
	                      AccelerometerPane accPane, GPSPane gpsPane, NFCPane nfcPane, int[] opacities) {

		super(userIDs, beginDate, endDate, dayBeginDate, dayEndDate, showCharts, saveCharts);

		this.listLayersModel = listLayersModel;
		this.accPane = accPane;
		this.gpsPane = gpsPane;
		this.nfcPane = nfcPane;
		this.opacities = opacities;
	}

	public void createCharts() {
		for (int userID : userIDs) {
			createCharts(userID);
		}
	}

	private void createCharts(int userID) {
		FrameCombinedBlock frameCombinedBlock = new FrameCombinedBlock(days, listLayersModel, opacities);
		for (Object object : listLayersModel.toArray()) {
			switch ((String) object) {
				case "Accelerometer":

					/*accelerometerReport = new AccelerometerReport(
							userIDs,
							accPane.dIntensityFrom.getDate(),
							accPane.dIntensityTo.getDate(),
							(Date) accPane.sIntervalFrom.getValue(),
							(Date) accPane.sIntervalTo.getValue(),
							(Float) accPane.cmbAdvancedAlpha.getSelectedItem());*/

					accelerometerReport = new AccelerometerReport(tsBegin, tsEnd, beginHour, beginMinute, beginSecond,
							endHour, endMinute, endSecond, days, periods, (Float) accPane.cmbAdvancedAlpha.getSelectedItem());

					accelerometerReport.setIntensitySettings(
							Integer.valueOf(accPane.txtAdvancedEpoch.getText()),
							Integer.valueOf(accPane.txtAdvancedMinimum.getText()),
							Integer.valueOf(accPane.txtSedentaryFrom.getText()),
							Integer.valueOf(accPane.txtLightFrom.getText()),
							Integer.valueOf(accPane.txtModerateFrom.getText()),
							Integer.valueOf(accPane.txtHardFrom.getText()));

					frameCombinedBlock.setAccelerometerData(
							getAccelerometerValues(userID),
							accPane.btnColorNoData.getBackground(),
							accPane.btnColorNoMovement.getBackground(),
							accPane.btnColorSedentary.getBackground(),
							accPane.btnColorLight.getBackground(),
							accPane.btnColorModerate.getBackground(),
							accPane.btnColorHard.getBackground());
					break;
				case "GPS":
					frameCombinedBlock.setGPSData(
							getGPSValues(userID),
							Color.RED,
							Color.BLUE,
							Color.GREEN
					);
					break;
				case "NFC":
					
					nfcReport = new NFCReport(tsBegin, tsEnd, beginHour, beginMinute, beginSecond,
							endHour, endMinute, endSecond, days, periods, nfcPane.getProfileTags());

					frameCombinedBlock.setNFCData(
							getNFCValues(userID),
							nfcReport.nfcTags,
							nfcPane.btnColorNoData.getBackground()
					);
					break;
			}
		}

		frameCombinedBlock.plotData();
		frameCombinedBlock.pack();

		if (showCharts) {
			frameCombinedBlock.setVisible(true);
		}
		else {
			frameCombinedBlock.dispose();
		}
	}

	private double[][] getAccelerometerValues(int userID) {
		accelerometerReport.getActivityCounts(userID);
		return accelerometerReport.getBlockValues(userID);
	}

	private double[][] getGPSValues(int userID) {
		//TODO
		return new double[3][0];
	}

	private double[][] getNFCValues(int userID) {
		return nfcReport.getValues(userID);
	}
}
